var isLoadingPopup = false;
// This function for loading images automatic 
function ADV_MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=ADV_MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function ADV_MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function ADV_MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=ADV_MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function ADV_MM_swapImage() { //v3.0
  var i,j=0,x,a=ADV_MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=ADV_MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// This function use for open 1 banner on page
function openBanner(host, divName, popDivName, viewImage, bannerIdx, bannersVar, urlsVar, viewurlsVar, clickurlsVar, 
					widthsVar, heightsVar, popbannersVar, popWidthsVar, popHeightsVar, delaysVar)
{
	banners		= eval(bannersVar); 
	urls		= eval(urlsVar);
	viewurls	= eval(viewurlsVar);
	clickurls	= eval(clickurlsVar);
	popbanners	= eval(popbannersVar);
	delays		= eval(delaysVar);
	widths		= eval(widthsVar);
	heights		= eval(heightsVar);
	popWidths		= eval(popWidthsVar);
	popHeights		= eval(popHeightsVar);
	divBanner		= document.getElementById(divName);
	divPopBanner	= document.getElementById(popDivName);
	divImage		= document.getElementById(viewImage);
	nextIdx = bannerIdx;
	
	//if (isLoadingPopup)
	//	closePopupBanner(popDivName);
	
	if (nextIdx==banners.length-1)
		nextIdx = 0;
	else
		nextIdx++;
	
	divBanner.innerHTML	= buildBannerContent(divName, popDivName, host, banners[bannerIdx], urls[bannerIdx], viewurls[bannerIdx], clickurls[bannerIdx],
									widths[bannerIdx], heights[bannerIdx], popbanners[bannerIdx], 
									popWidths[bannerIdx], popHeights[bannerIdx], false); // + "<img src='"+host+"/images/blank.gif' height=2 width="+widths[bannerIdx]+">";
	//divBanner.height = heights[bannerIdx]+2;
	divImage.innerHTML = "<img src='"+viewurls[bannerIdx]+"' border=0 width=0 height=0>";
	
	self.setTimeout("openBanner('"+host+"', '"+divName+"', '"+popDivName+"', '"+viewImage+"', "+nextIdx+", '" + bannersVar + "', '" + urlsVar + "', '" + 
								viewurlsVar + "', '" + clickurlsVar + "', '" + widthsVar + "', '" + 
								heightsVar + "', '" + popbannersVar + "', '" + popWidthsVar + "', '" + popHeightsVar + "', '" + 
								delaysVar + "')", delays[bannerIdx]*1000);
}

// This function use for build a banner content into div
function buildBannerContent(bannerName, popDivName, host, banner, url, viewurl, clickurl, width, height, popbanner, popWidth, popHeight, isPopup)
{
	var bannerContent = "";
	var file	= banner;
	var onMouseOver = "";
	var onMouseOut = "";
	if (popbanner!="" && !isPopup)
		onMouseOver = "onmouseover=\"startLoadPopupBanner('"+bannerName + "', '" + popDivName + "', '" + host + "', '" + clickurl + "', " + width + ", '" + height + "', '" + popbanner + "', " + popWidth + ", " + popHeight + ");\"";
	//if (isPopup)
	//	onMouseOut = "onmouseout=\"closePopupBanner('"+bannerName+"');\"";
	if (banner.lastIndexOf('.flv')<0 && banner.lastIndexOf('.swf')<0) 
	{
		if (clickurl!="") 
		{
			bannerContent = "<a href='#' onClick=\"openURL('"+clickurl+"');\" "+onMouseOver+onMouseOut+"><img width='"+width+"' height='"+
								height+"' src='" + file + "' border=0\><a>";
		}
		else
		{
			bannerContent = "<img width='"+width+"' height='" + height+"' src='" + file + "' border=0  "+onMouseOver+onMouseOut+"\>";
		}
	}
	else
	{
		var flashHREF = '';
		if (clickurl!="")
		{
			// onClick=\"openURL('"+clickurl+"');\"
			flashHREF = "<a href='#' onClick=\"openURL('"+clickurl+"');\" style='padding:0;margin:0;position:absolute;' "+onMouseOver+onMouseOut+"> " +
							"<img id='topImage' src='"+host+ "images/blank.gif' width='"+width+"' height='"+height+"' border=0 /></a>";
		} else if (popbanner!="")
		{
			flashHREF = "<img id='topImage' src='"+host+ "images/blank.gif' width='"+width+"' height='"+height+"' border=0 "+onMouseOver+onMouseOut+"/>";
		}
		bannerContent = flashHREF + "<object  style='z-index:0;' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0'"+
						"width='"+width+"' height='"+height+"'>"+
						"<param name='movie' value='"+file+"'>"+
						"<PARAM NAME='WMode' VALUE='Transparent'>"+
						"<param name='quality' value='high'>"+
						"<param name='loop' value='false'>"+
						"<param name='wmode' value='transparent'>"+
						"<embed src='"+file+"' quality='high' "+
							"width='"+width+"' wmode='Transparent' style='z-index:0;' scale='showall' wmode='transparent' height='"+height+"' loop='true' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'>"+
						"</embed>"+
					"</object>";
	}
	
	return bannerContent;
}

function getBannerPosition(bannerName)
{
	var banner = document.getElementById(bannerName);
	var posX = 0;
	var posY = 0;
	while (banner.offsetParent!=null) {
		posY += banner.offsetTop;
		posX += banner.offsetLeft;
		banner = banner.offsetParent;
	}
	return Array(posX, posY);
}

function showPopupBanner(bannerName, popDivName, host, clickurl, width, height, popbanner, popWidth, popHeight)
{
	var innerHeight = window.innerHeight;
	var innerWidth = window.innerWidth;
	
	var popBanner = document.getElementById(popDivName);
	var popBannerClose = document.getElementById(popDivName+"Close");
	popBanner.style.opacity = "1";
	//alert(popBanner.offsetTop);
	posY = popBanner.offsetTop;
	posX = popBanner.offsetLeft;
	if (posY+popHeight-window.scrollY>innerHeight)
		posY = (posY - popHeight + height);
	popBanner.style.height = popHeight;
	
	if (posX+popWidth>innerWidth)
		posX = (posX - popWidth + width);
	popBanner.style.width = popWidth;	
	
	popBanner.style.left= (posX-1) + "px";
	popBanner.style.top = (posY) + "px";
	
	popBannerClose.style.left = (popBanner.offsetLeft + popWidth - 10) + "px";
	popBannerClose.style.top = (popBanner.offsetTop + 3) + "px";
	popBannerClose.style.visibility = "visible";
	
	//alert(popbanner);
	//  onmouseout=\"closePopupBanner('"+popDivName+"');\"
	popBanner.innerHTML = "<table align=\"center\"  border=\"1\" style=\"border:thick;border-color:#CFD5D8\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>" + 
							buildBannerContent(popDivName, "", host, popbanner, "", "", clickurl, popWidth, popHeight, "", 0, 0, true)+
						  "</td></tr></table>";
	//"<img src='"+popbanner+"' border=0 width='"+popWidth+"' height='"+popHeight+"'>";
	
}

function startLoadPopupBanner(bannerName, popDivName, host, clickurl, width, height, popbanner, popWidth, popHeight)
{
	//console.log("Start loading: "+loadingPopupName);
	if (!isLoadingPopup) {
		isLoadingPopup = true;
		loadingPopupBanner(bannerName, popDivName, host, clickurl, width, height, popbanner, popWidth, popHeight, 1);
		//alert(popbanner);
	}
}

function loadingPopupBanner(bannerName, popDivName, host, clickurl, width, height, popbanner, popWidth, popHeight, i)
{
	var popBanner = document.getElementById(popDivName);
	var popBannerCount = document.getElementById(popDivName+"Count");
	if (i==1)
	{
		var bannerPos = getBannerPosition(bannerName);
		//console.log(bannerPos);
		popBanner.style.left= bannerPos[0] + "px";
		popBanner.style.top = bannerPos[1] + "px";
		popBanner.style.width = width;
		popBanner.style.height= height;
		popBanner.style.visibility = "visible";
		popBanner.style.opacity = ".8";
		
		popBannerCount.style.left= bannerPos[0] + "px";
		popBannerCount.style.top = bannerPos[1] + "px";
		popBannerCount.style.width = width;
		popBannerCount.style.height= height;
		popBannerCount.style.visibility = "visible";
		popBannerCount.style.opacity = ".8";
	}
	if (i<=3 && isLoadingPopup)
	{
		// onmouseout=\"closePopupBanner('"+popDivName+"');\"
		var image = "<img  onmouseout=\"closePopupBanner('"+popDivName+"');\" src='http://localhost/advmanagement/images/blank.gif' border=0 width='" + width+ "' height='" + height+ "'>";
		popBannerCount.innerHTML = "<table width='"+width+"' height='"+height+"' border=0  cellpadding='0' cellspacing='0'><tr><td align=center valign=middle width='100%' height='100%' style=\"font-family:'Arial Black', Gadget, sans-serif\"><font size=+3 color=white><strong><i>"+i+"</i></strong></font></td></tr></table>";
		
		popBanner.innerHTML = image;
		i++;

		self.setTimeout("loadingPopupBanner('"+bannerName+ "', '" + popDivName+ "', '" + host+ "', '" + clickurl+ "', " + width+ ", " + height+ ", '" + popbanner+ "', " + popWidth+ ", " + popHeight+ ", " + i + ")", 1000);
	} else if (isLoadingPopup)
	{
		if (popBanner.style.visibility=="visible")
		{
			showPopupBanner(bannerName, popDivName, host, clickurl, width, height, popbanner, popWidth, popHeight);
			popBannerCount.innerHTML = "";
			popBannerCount.style.visibility = "hidden";
		}
	}
}

function closePopupBanner(popBannerName)
{
	var popBanner = document.getElementById(popBannerName);
	var popBannerCount = document.getElementById(popBannerName+"Count");
	var popBannerClose = document.getElementById(popBannerName+"Close");
	popBanner.style.visibility = "hidden";
	popBanner.innerHTML = "";
	popBanner.style.width = 0;
	popBanner.style.height= 0;
	isLoadingPopup = false;
	
	popBannerCount.style.visibility = "hidden";
	popBannerCount.innerHTML = "";
	popBannerCount.style.width = 0;
	popBannerCount.style.height= 0;
	
	popBannerClose.style.visibility = "hidden";
	
	isLoadingPopup = false;
}
// This function use for popup 1 new window with passed url
function openURL(url) {
	var w = screen.width;
	var h = screen.height;
	//winprops = 'height='+h+',width='+w+',top=0,left=0,scrollbars=yes,resizable=yes';
	
	win = window.open(url, url);//, winprops);
	
	return win;
}